<?php 

class Register extends Model{
	function Register(){
		parent::Model();
	}

	function insert($table,$contents){
		$this->db->insert($table, $contents);
	}

	function find_unconfirmed_user($email){
		$this->db->from('signup');
		$this->db->where('email', $email);
		$query=$this->db->get();
		return $query->row_array();
	}

	function confirm($id, $activation_code){
		$this->db->from('signup');
		$this->db->where('userid', $id);
		$this->db->where('activation_code', $activation_code);
		$query = $this->db->get();
		if($query->num_rows()==0){
			return FALSE;
		}
		else{
			return $query->row_array();
		}
	}

	function auth($table, $username, $password, $salt){
		$this->db->from($table);
		$this->db->where('email', $username);
		$this->db->where('salt', $salt);
		$this->db->where('password', sha1($salt.$password));
		$query = $this->db->get();
		return $query->row_array();
	}

	function delete($id){
		return $this->db->delete('signup', array('userid' => $id));
	}

	function selectStudent($userid){
		$this->db->from('students');
		$this->db->where('students.userid', $userid);
		$query = $this->db->get();
		return $query->row_array();
	}
	
	function selectTeacher($userid){
		$this->db->from('teachers');
		$this->db->where('teachers.userid', $userid);
		$query = $this->db->get();
		return $query->row_array();
	}

	function update($user, $userid){
		return $this->db->update('students', $user, array('userid'=>$userid));
	}

	function updateTeacher($user, $userid){
		return $this->db->update('teachers', $user, array('userid'=>$userid));
	}

	function getSalt($email, $table){
		$this->db->select('salt');
		$this->db->from($table);
		$this->db->where('email', $email);
		$query = $this->db->get()->row_array();
		return $query['salt'];
	}

	function last_login($table, $email){
		$date = date('Y-m-d H:i:s', now());
		$data = array('last_login' => $date);

		$this->db->where('email', $email);
		$this->db->update('students', $data);
	}
}

?>
